<template>
	<view>
		<view class="title"><!-- <image src="../../static/title.png" mode=""></image> --></view>
		<view v-for="(wj, i) in wenjuanList" :key="i">
			<wenjuan
				class="test"
				:hasOther="wj.hasOther"
				:type="wj.type"
				:value="wj.value"
				:maxSelect="wj.maxSelect"
				:title="wj.title"
				:subTitle="wj.subTitle"
				:defaultValue="wj.defaultValue"
				@change="change($event, wj.id, wj.name)"
				@send="sendCode($event, wj.id, wj.name)"
			></wenjuan>
		</view>
		<view id="submitButton" @tap="submit">提交问卷(无后台)</view>
	</view>
</template>

<script>
import wenjuan from '@/components/gwh-wenjuan/gwh-wenjuan.vue';
export default {
	components: {
		wenjuan
	},
	data() {
		return {
			wenjuanList: [
				{
					id: '1',
					name: 'question1',
					type: 'input',
					title: '您的真实姓名：',
					subTitle: '',
					maxSelect: 10,
					defaultValue: '请输入您的真实姓名'
				},
				{
					id: '2',
					name: 'question1',
					type: 'input',
					title: '您的手机号码：',
					subTitle: '',
					maxSelect: 11,
					defaultValue: '请输入您的手机号码'
				},
				{
					id: '3',
					name: 'question1',
					type: 'input',
					title: '公司名称：',
					subTitle: '',
					maxSelect: 10,
					defaultValue: '请输入您的公司名称'
				},
				{
					id: '4',
					name: 'question9',
					type: 'region',
					title: '请选择公司地址',
					subTitle: '',

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '5',
					name: 'question1',
					type: 'input',
					title: '详细地址：',
					subTitle: '',
					maxSelect: 10,
					defaultValue: '请输入您的公司详细名称'
				},
				{
					id: '6',
					name: '问题六',
					type: 'single-picker',
					title: '公司性质',
					subTitle: '',
					value: ['厂家'],
					defaultValue: '0',

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '7',
					name: '希望合作城市',
					type: 'multi-select',
					title: '希望合作城市',
					subTitle: '',
					value: [
						'徐州'
						// "厦门",
						// "深圳",
						// "广州"
					],
					defaultValue: [],
					maxSelect: 2,
					hasOther: false,
					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '8',
					name: '问题八',
					type: 'single-picker',
					title: '品类范围',
					subTitle: '',
					value: ['蔬菜'],
					defaultValue: '0',

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '9',
					name: 'question1',
					type: 'input',
					title: '售卖商品：',
					subTitle: '',
					maxSelect: 10,
					defaultValue: '请输入售卖商品'
				},
				{
					id: '10',
					name: 'question6',
					type: 'textarea',
					title: '生产及供货能力描述',
					subTitle: '',
					maxSelect: 200,
					defaultValue: '请输入生产及供货能力描述',

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '11',
					name: 'question1',
					type: 'input',
					title: '合作品牌：',
					subTitle: '',
					maxSelect: 10,
					defaultValue: '请输入合作品牌'
				},
				{
					id: '12',
					name: 'question10',
					type: 'image',
					title: '请您上传【经营商品的各类许可证明】：',
					subTitle: '',
					maxSelect: 3,

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '13',
					name: 'question10',
					type: 'image',
					title: '请您上传【权威机构的商品质检报告】：',
					subTitle: '',
					maxSelect: 3,

					//用于接收已选择的数据
					resultValue: []
				},
				{
					id: '14',
					name: 'question10',
					type: 'image',
					title: '请您上传【其他证明】：',
					subTitle: '',
					maxSelect: 3,

					//用于接收已选择的数据
					resultValue: []
				}
			]
		};
	},
	onLoad() {
		this.showQi();
		this.showQw();
	},
	methods: {
		showQi() {
			this.$forceUpdate();
			let that = this;
			that.$api
				.request(
					'admin.merchant',
					'getConfigByCategory',
					{
						adminId: 1
					},
					failres => {
						// that.logining = false
						that.$api.msg(failres.errmsg);
					}
				)
				.then(res => {
					this.wenjuanList[5].value = res.data;
					// console.log(res)
				});
		},
		showQw() {
			this.$forceUpdate();
			let that = this;
			that.$api
				.request(
					'admin.merchant',
					'getConfigByCategory',
					{
						adminId: 2
					},
					failres => {
						// that.logining = false
						that.$api.msg(failres.errmsg);
					}
				)
				.then(res => {
					this.wenjuanList[7].value = res.data;
					// console.log(res)
				});
		},
		change(e, ...res) {
			// console.log(e);
			// console.log(res);

			//用于更新数据
			for (var i = 0; i < this.wenjuanList.length; i++) {
				if (this.wenjuanList[i].id == res[0]) {
					this.wenjuanList[i].resultValue = e;
				}
			}
		},
		sendCode(e, ...res) {
			console.log('please send check code to the phone:' + e[0]);
			console.log(res);
		},
		submit() {
			var c = {};
			for (var i = 0; i < this.wenjuanList.length; i++) {
				// console.log("问题标号: "+this.wenjuanList[i].id);
				// console.log("用户答案: ");

				// console.log();
				if (this.wenjuanList[0].resultValue[0] == '') {
					uni.showToast({
						title: '请输入真实姓名',
						icon: 'none'
					});
					return;
				}
				if (!/^1[3456789]\d{9}$/.test(this.wenjuanList[1].resultValue[0])) {
					uni.showToast({
						title: '请输入正确的手机号',
						duration: 2000,
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[2].resultValue[0] == '') {
					uni.showToast({
						title: '请输入公司名称',
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[6].resultValue[0] == undefined) {
					// console.log(111);
					uni.showToast({
						title: '请选择希望合作城市',
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[8].resultValue[0] == '') {
					uni.showToast({
						title: '请输入售卖产品',
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[9].resultValue[0] == '') {
					uni.showToast({
						title: '请输入生产及提供能力描述',
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[10].resultValue[0] == '') {
					uni.showToast({
						title: '请输入合作品牌',
						icon: 'none'
					});
					return;
				}
				if (this.wenjuanList[11].resultValue.length == 0) {
					uni.showToast({
						title: '请您上传【经营商品的各类许可证明】：',
						icon: 'none'
					});
				}
				if (this.wenjuanList[12].resultValue.length == 0) {
					uni.showToast({
						title: '请您上传【权威机构的商品质检报告】：',
						icon: 'none'
					});
				}

				c.supplierName = this.wenjuanList[0].resultValue[0];
				c.supplierPhone = this.wenjuanList[1].resultValue[0];
				c.companyName = this.wenjuanList[2].resultValue[0];
				c.companyAddress = `${this.wenjuanList[3].resultValue[0]}${this.wenjuanList[4].resultValue[0]}`;
				c.enterpriseProperty = this.wenjuanList[5].resultValue[0];
				c.collaborateCity = this.wenjuanList[6].resultValue[0];
				c.category = this.wenjuanList[7].resultValue[0];
				c.product = this.wenjuanList[8].resultValue[0];
				c.description = this.wenjuanList[9].resultValue[0];
				c.cooperativeBrand = this.wenjuanList[10].resultValue[0];
				c.prov = this.wenjuanList[11].resultValue.join(',');
				c.qualityReport = this.wenjuanList[12].resultValue.join(',');
				c.others = this.wenjuanList[13].resultValue.join(',');
				// c.company_name=this.wenjuanList[2].resultValue[0]
				//do something else
			}
			let that = this;

			that.$api
				.request(
					'admin.supplier',
					'create',
					{
						activityDTO: JSON.stringify(c)
					},
					failres => {
						// that.logining = false
						that.$api.msg(failres.errmsg);
					}
				)
				.then(res => {
					// this.wenjuanList[7].value=res.data
					console.log(res);
					uni.showToast({
						title: '提交成功',
						icon: 'none'
					});
					setTimeout(() => {
						uni.switchTab({
							url: '../user/user'
						});
					}, 1000);
					// uni.navigateTo({
					// 	url:'success/success'
					// })
					// console.log(res)
				});
		}
	}
};
</script>

<style lang="scss">
.title {
	width: 100%;
	height: 150rpx;
	// background-color: red;
	margin-bottom: 20rpx;
	image {
		width: 100%;
		height: 100%;
	}
}
#submitButton {
	background-color: #3090d9;
	color: #ffffff;
	width: 710upx;
	padding: 10upx;
	margin-left: 10upx;
	text-align: center;
	margin-top: 15upx;
	margin-bottom: 15upx;
	border-radius: 10upx;
}
</style>
